/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ipfms.stripes;

import com.google.inject.Inject;
import ipfms.dao.BuildingImgDAO;
import ipfms.model.BuildingImg;
import ipfms.stripes.util.Configuration;
import java.io.File;
import java.io.FileInputStream;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ErrorResolution;
import net.sourceforge.stripes.action.HttpCache;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.UrlBinding;


/**
 *
 * @author fikri
 */
@HttpCache(allow = true)
@UrlBinding("/dokumen/view/{idImg}")
public class ViewDocument extends BaseActionBean{
    
    @Inject
    private Configuration conf;
    
    @Inject private BuildingImgDAO buildingImageDAO;
    
    private Integer idImg;
    
    @DefaultHandler
    public Resolution view() {      

        String docPath = conf.getProperty("document.path");
        if (docPath == null) {
            return new ErrorResolution(500,
                    "Konfigurasi \"document.path\" tidak dijumpai");
        }
        
        BuildingImg img = buildingImageDAO.findById(idImg);        

        String path = docPath + (docPath.endsWith(File.separator) ? "" : File.separator) + img.getImgPath();
        File f = new File(path);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
            
        } catch (Exception e) {
            
            return new ErrorResolution(500,
                    "Fail " + path + " tidak dijumpai");
        }               

        return new StreamingResolution(img.getFormat(), fis);

    }

    public Integer getIdImg() {
        return idImg;
    }

    public void setIdImg(Integer idImg) {
        this.idImg = idImg;
    }
    
    
}
